package com.yzt.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzt.base.kit.Base;
import com.yzt.sys.entity.Role;
import com.yzt.sys.kit.RoleKit;
import com.yzt.sys.service.IRoleService;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Y
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {

	@Autowired
	private IRoleService roleService;

	@GetMapping("list")
	public Base<PageInfo<Role>> list(RoleKit kit) {
		PageInfo<Role> pageInfo = PageHelper.startPage(kit.getPage(), kit.getLimit())
				.doSelectPageInfo(() -> roleService.list(Wrappers.<Role>lambdaQuery()
						.like(StringUtils.isNotEmpty(kit.getName()), Role::getName, kit.getName())));
		return Base.okData(pageInfo);
	}

	@PostMapping("")
	public Base<Object> save(Role role) {
		if (roleService.save(role)) {
			return Base.ok();
		}
		return Base.fail();
	}

	@PutMapping("")
	public Base<Object> update(Role role) {
		if (roleService.updateById(role)) {
			return Base.ok();
		}
		return Base.fail();
	}

	@DeleteMapping("{id}")
	public Base<Object> delete(@PathVariable("id") long id) {
		if (roleService.removeById(id)) {
			return Base.ok();
		}
		return Base.fail();
	}

	@GetMapping("{id}")
	public Base<Role> get(@PathVariable("id") long id) {
		Role role = roleService.getById(id);
		if (role != null)
			return Base.okData(role);
		return Base.fail();
	}

}
